package com.quanyan.ic.datacenter.http.service.impl;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.common.http.exception.BizException;
import com.quanyan.ic.datacenter.constant.Constants;
import com.quanyan.ic.datacenter.dao.*;
import com.quanyan.ic.datacenter.hik8710.service.Hik8710Service;
import com.quanyan.ic.datacenter.http.service.PlaceUnitAlarmService;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitVideoAlarm;
import com.quanyan.ic.datacenter.mapper.icData.entity.PlaceUnitVideoAlarmHandle;
import com.quanyan.ic.datacenter.model.AlarmDayReportParam;
import com.quanyan.ic.datacenter.model.Hik8710EventAlarm;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhuo.shi on 2017/8/28.
 */
@Service
public class PlaceUnitAlarmServiceImpl implements PlaceUnitAlarmService {

    @Autowired
    private PlaceUnitVideoAlarmDao placeUnitVideoAlarmDao;
    @Autowired
    private PlaceDao placeDao;
    @Autowired
    private PlaceUnitVideoAlarmHandleDao placeUnitVideoAlarmHandleDao;
    @Autowired
    private Hik8710Service hik8710Service;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private AlarmDayReportOfCityDao alarmDayReportOfCityDao;
    @Autowired
    private AlarmDayReportOfPlaceDao alarmDayReportOfPlaceDao;
    @Autowired
    private AlarmTimePointReportOfCityDao alarmTimePointReportOfCityDao;
    @Autowired
    private AlarmTimePointReportOfPlaceDao alarmTimePointReportOfPlaceDao;

    @Transactional
    @Override
    public void addOrUpdatePlaceUnitAlarm(Hik8710EventAlarm alarm) {
        Integer alarmUuid = Integer.valueOf(alarm.getUuid());
        PlaceUnitVideoAlarm videoAlarm = placeUnitVideoAlarmDao.exist(alarmUuid);
        if (videoAlarm != null){
            videoAlarm.setEventId(alarm.getEventId());
            videoAlarm.setEventType(alarm.getEventType());
            videoAlarm.setResIndex(alarm.getRes_index());
            try {
                videoAlarm.setEndTime(sdf.parse(alarm.getEndTime()));
                videoAlarm.setEventIds(JSON.toJSONString(alarm.getEventIds()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            placeUnitVideoAlarmDao.updatePlaceUnitVideoAlarm(videoAlarm);

            PlaceUnitVideoAlarmHandle handle = placeUnitVideoAlarmHandleDao.findByUuid(alarmUuid);
            if (handle == null){
                throw new BizException("can't find PlaceUnitVideoAlarmHandle of alarmUuid=" + alarmUuid);
            }
            try {
                Date endTime = sdf.parse(alarm.getEndTime());
                Date startTime = sdf.parse(alarm.getStartTime());
                handle.setEndTime(endTime);
                handle.setDuration((int)(endTime.getTime()-startTime.getTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            handle.setScreenshotUrls(JSON.toJSONString(hik8710Service.getEventImgUrl(alarm.getEventIds())));
            placeUnitVideoAlarmHandleDao.updatePlaceUnitVideoAlarmHandle(handle);
            return;
        }
        videoAlarm = new PlaceUnitVideoAlarm();
        videoAlarm.setAlarmUuid(alarmUuid);
        PlaceUnitInfo placeUnitInfo = placeDao.getPlaceUnitInfo(alarm.getPlaceUnitId());
        if (placeUnitInfo == null){
            throw new BizException("can't find placeUnit " + alarm.getPlaceUnitId() + " info");
        }
        BeanUtils.copyProperties(placeUnitInfo, videoAlarm);
        videoAlarm.setEventId(alarm.getEventId());
        videoAlarm.setEventType(alarm.getEventType());
        videoAlarm.setResIndex(alarm.getRes_index());
        try {
            videoAlarm.setStartTime(sdf.parse(alarm.getStartTime()));
            videoAlarm.setEndTime(sdf.parse(alarm.getEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        videoAlarm.setEventIds(JSON.toJSONString(alarm.getEventIds()));
        placeUnitVideoAlarmDao.insertPlaceUnitVideoAlarm(videoAlarm);

        PlaceUnitVideoAlarmHandle handle = new PlaceUnitVideoAlarmHandle();
        BeanUtils.copyProperties(videoAlarm, handle);
        handle.setDuration((int)(videoAlarm.getEndTime().getTime()-videoAlarm.getStartTime().getTime()));
        String screenshotUrls = JSON.toJSONString(hik8710Service.getEventImgUrl(alarm.getEventIds()));
        handle.setScreenshotUrls(screenshotUrls);
        placeUnitVideoAlarmHandleDao.insertPlaceUnitVideoAlarmHandle(handle);

        long between=( videoAlarm.getEndTime().getTime() - videoAlarm.getStartTime().getTime())/1000;//除以1000是为了转换成秒
        Long min =between/60;;

        AlarmDayReportParam alarmDayReportParam = new AlarmDayReportParam();
        alarmDayReportParam.setCityId(videoAlarm.getCityId());
        alarmDayReportParam.setDate(videoAlarm.getStartTime());
        alarmDayReportParam.setType(Constants.AlarmsType.alarms_type_video);
        alarmDayReportParam.setMin(min.intValue());
        alarmDayReportParam.setPlaceId(videoAlarm.getPlaceId());
        alarmDayReportParam.setPlaceName(videoAlarm.getPlaceName());
        alarmDayReportParam.setStartTimeInt(videoAlarm.getStartTime().getHours());
        alarmDayReportOfCityDao.consumerOrder(alarmDayReportParam);
        alarmDayReportOfPlaceDao.consumerOrder(alarmDayReportParam);
        alarmTimePointReportOfCityDao.insertAlarmTimePointCity(alarmDayReportParam);
        alarmTimePointReportOfPlaceDao.insertAlarmTimePointPlace(alarmDayReportParam);


    }
}
